package com.example.campus;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.campus.utils.WebViewUtils;
import com.news.m.AbNewsRequest;
import com.news.m.InfoAnnoucement;
import com.news.m.NewsDAO;
import com.news.m.NewsRequestEngine;

public class NewsListView  implements OnScrollListener,OnItemClickListener {
	ArrayList<NewsDAO>list;
	Context context;
	ListView listview;
	NewsBaseAdapter base;
	AbNewsRequest request=new NewsRequestEngine();
	public NewsListView(int position,Context context)
	
	{//<div id="xaufe_middiv">
		if(position==0){
			request=new InfoAnnoucement();
		}
		else{
			request=new NewsRequestEngine();
			((NewsRequestEngine)request).id=position+10-1;;
		}
		this.list=new ArrayList<NewsDAO>();
		this.context=context;
		this.listview=new ListView(context);
//		Color="#fff1f1f1";
//		listview.setDivider(Drawable.cr)
		listview.setDividerHeight(1) ;
		listview.setOnItemClickListener(this);
		listview.setOnScrollListener(this);
	}
	
	@Override  
    public void onScrollStateChanged(AbsListView view, int scrollState){  
       
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {  
            
            if (view.getLastVisiblePosition() == view.getCount() - 1) {  
               
            	go();
            }  
        }  
    }  
	public ListView getView()
	{
		return listview;
	}
	void init()
	{
		base=new NewsBaseAdapter(context, list);
		listview.setAdapter(base);
	go();
		
		
	}
	void go()
	{
		new Thread()
		{
			public void run()
			{
				
				try {
					final ArrayList<NewsDAO> reqlist=request.next();
					if(reqlist!=null&&reqlist.size()!=0)
					{
						Handler h=new Handler(Looper.getMainLooper());
						h.post(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								list.addAll(reqlist);
								base.notifyDataSetChanged();
							}
						});
						
						
					}
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}.start();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		try {
			Web.run(context,(list.get(position).url));
		}  catch (Exception e) {
			// TODO Auto-generated catch block
			((Activity)context).setTitle("无法完成");
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		
	}
	
}
